<?php

	/*
	 * To change this license header, choose License Headers in Project Properties.
	 * To change this template file, choose Tools | Templates
	 * and open the template in the editor.
	 */

	namespace Phptricks\app\controllers\trieds;

	/**
	 * Description of trieds
	 *
	 * @author Abd Elfttah Ahmed <phptricks.net@gmail.com>
	 */
	use Phptricks\app\controllers\base\base as base;
	use Phptricks\app\models\groups\groups as groups_model;
	use Phptricks\app\models\users\users as users_model;
	use Phptricks\app\models\trieds\trieds as trieds_model;

	class trieds extends base{

		public $groups_model = null;
		public $users_model = null;
		public $trieds_model = null;
		public $countrys_model = null;
		private $tried_fields = null;

		public function __construct(){
			parent::__construct();
			$this->tried_fields = array(
				'tried_id',
				'tried_user_id',
				'tried_type',
				'tried_text',
				'tried_tags',
				'tried_like',
				'tried_comment',
				'tried_view',
				'tried_datetime',
			);
			$this->users_model = users_model::getinstance();
			$this->trieds_model = trieds_model::getinstance();

			$this->view->assign('page_title', $this->lang['trieds_controll']);
		}

		public function index(){
			$this->view->display('trieds.tpl');
		}

		public function admin_index($order_by = 'tried_id', $sort_type = 'DESC', $page = '1'){
			$this->admin_view($order_by, $sort_type, $page);
		}

		public function admin_view($order_by = 'tried_id', $sort_type = 'DESC', $page = '1'){
			if (\in_array($order_by, $this->tried_fields)) {
				if (\strtoupper($sort_type) == 'ASC') {
					$sort_type = 'ASC';
				} else {
					$sort_type = 'DESC';
				}
				if (!empty($order_by)) {
					$order_by = $order_by . " " . $sort_type;
				} else {
					$order_by = '';
				}
			} else {
				$order_by = '';
			}
			$array_wheres = array(
				'users.user_id|number' => 'trieds.tried_user_id'
			);
			$page = intval($page);
			$_CountRow = $this->trieds_model->_CountRow('trieds, users', $array_wheres, '');
			$this->pagination->_set_pagination($_CountRow, $page, 20, 15);
			$data = $this->trieds_model->get_trieds('', $order_by, 'trieds.tried_id', $this->pagination->_limit);
			$this->view->assign('count_data', $_CountRow);
			$this->view->assign('pagination', $this->pagination->_pages);
			$this->view->assign('count_page', $this->pagination->_count_pager);
			$this->view->assign('current_page', $this->pagination->_current_page);
			$this->view->assign('first_page', $this->pagination->_first_page);
			$this->view->assign('last_page', $this->pagination->_last_page);
			if (empty($order_by)) {
				$order_by = 'tried_id/' . $sort_type;
			} else {
				$order_by = \str_replace(' ', '\\', $order_by);
			}
			$this->view->assign('page_link', 'admin/trieds/view/' . $order_by . '/');
			if (\strtoupper($sort_type) == 'DESC') {
				$sort_type = 'ASC';
			} else {
				$sort_type = 'DESC';
			}
			$this->view->assign('sort_type', $sort_type);
			$this->view->assign('data', $data);
			$this->view->display('trieds.tpl');
		}

		public function admin_user($user_id, $page = '1', $order_by = 'tried_id', $sort_type = 'DESC'){
			$user_id = \intval($user_id);
			if (empty($user_id)) {
				$this->display_message('danger', $this->lang['error_404'], 'admin/trieds/index/', 10);
			}
			if (\in_array($order_by, $this->tried_fields)) {
				if (\strtoupper($sort_type) == 'ASC') {
					$sort_type = 'ASC';
				} else {
					$sort_type = 'DESC';
				}
				if (!empty($order_by)) {
					$order_by = $order_by . " " . $sort_type;
				} else {
					$order_by = '';
				}
			} else {
				$order_by = '';
			}
			$array_where = array(
				'trieds.tried_user_id' => $user_id
			);
			$_CountRow = $this->trieds_model->_CountRow('trieds', $array_where);
			$page = intval($page);
			$this->pagination->_set_pagination($_CountRow, $page, 20, 15);
			$data = $this->trieds_model->get_trieds($array_where, $order_by, 'trieds.tried_id', $this->pagination->_limit);
			$this->view->assign('user_id', $user_id);
			$this->view->assign('count_data', $_CountRow);
			$this->view->assign('pagination', $this->pagination->_pages);
			$this->view->assign('count_page', $this->pagination->_count_pager);
			$this->view->assign('current_page', $this->pagination->_current_page);
			$this->view->assign('first_page', $this->pagination->_first_page);
			$this->view->assign('last_page', $this->pagination->_last_page);
			$this->view->assign('page_link', 'admin/trieds/user/' . $user_id . "/" . \str_replace(' ', '\\', $order_by) . '/');
			if (\strtoupper($sort_type) == 'DESC') {
				$sort_type = 'ASC';
			} else {
				$sort_type = 'DESC';
			}
			$this->view->assign('sort_type', $sort_type);

			$this->view->assign('data', $data);
			$this->view->display('trieds.tpl');
		}

		public function admin_inactive($order_by = 'tried_id', $sort_type = 'DESC', $page = '1'){
			if (\in_array($order_by, $this->tried_fields)) {
				if (\strtoupper($sort_type) == 'ASC') {
					$sort_type = 'ASC';
				} else {
					$sort_type = 'DESC';
				}
				if (!empty($order_by)) {
					$order_by = $order_by . " " . $sort_type;
				} else {
					$order_by = '';
				}
			} else {
				$order_by = '';
			}
			$array_wheres = array(
				'trieds.tried_active|number' => '1',
				'users.user_id|number' => 'trieds.tried_user_id'
			);
			$page = intval($page);
			$_CountRow = $this->trieds_model->_CountRow('trieds, users', $array_wheres, 'trieds.tried_id');
			$this->pagination->_set_pagination($_CountRow, $page, 20, 15);
			$data = $this->trieds_model->get_trieds($array_wheres, $order_by, 'trieds.tried_id', $this->pagination->_limit);
			$this->view->assign('count_data', $_CountRow);
			$this->view->assign('pagination', $this->pagination->_pages);
			$this->view->assign('count_page', $this->pagination->_count_pager);
			$this->view->assign('current_page', $this->pagination->_current_page);
			$this->view->assign('first_page', $this->pagination->_first_page);
			$this->view->assign('last_page', $this->pagination->_last_page);
			if (empty($order_by)) {
				$order_by = 'tried_id/' . $sort_type;
			} else {
				$order_by = \str_replace(' ', '\\', $order_by);
			}
			$this->view->assign('page_link', 'admin/trieds/inactive/' . $order_by . '/');
			if (\strtoupper($sort_type) == 'DESC') {
				$sort_type = 'ASC';
			} else {
				$sort_type = 'DESC';
			}
			$this->view->assign('sort_type', $sort_type);
			$this->view->assign('data', $data);
			$this->view->display('trieds.tpl');
		}

		public function admin_edit($id){
			$id = \intval($id);
			if (empty($id)) {
				$this->display_message('danger', $this->lang['error_select_data'], 'admin/trieds/view/', 5);
			}
			$array_where = array(
				'tried_id' => $id,
			);
			$data = $this->trieds_model->get_trieds($array_where);
			$this->view->assign('data', $data[0]);
			$this->view->display('trieds.tpl');
		}

		public function admin_update(){
			$this->form_validation->post(array(
				'tried_id' => 'isnull|digit',
				'tried_active' => 'isnull|digit',
				'tried_text' => 'isnull|strip_tags|maxlength:' . $this->settings['tried_maxlength'] . '|minlength:' . $this->settings['tried_minlength'],
				'tried_tags' => 'isnull',
				'tried_image' => '',
			));
			$this->form_validation->_array_data['tried_tags'] = $this->custom_fields->encode($this->form_validation->_array_data['tried_tags']);
			$this->form_validation->fetch();
			$submit = $this->form_validation->submit();
			if ($submit !== true) {
				$this->display_message('danger', \implode('<br>', $this->form_validation->_error), 'admin/trieds/edit/' . $this->form_validation->_array_data['tried_id'], 5);
			}
			$id = $this->form_validation->_array_data['tried_id'];
			$this->form_validation->unset_var('tried_id', 'post');
			$array_where = array(
				'tried_id' => $id,
			);
			$this->trieds_model->updatedata($this->form_validation->_array_data, $array_where);
			$this->display_message('success', $this->lang['success_edit_data'], 'admin/trieds/view/', 3);
		}

		public function admin_add(){
			$this->view->display('trieds.tpl');
		}

		public function admin_addnew(){
			$this->tried_fields = array(
				'tried_id',
				'tried_user_id',
				'tried_type',
				'tried_text',
				'tried_tags',
				'tried_like',
				'tried_comment',
				'tried_view',
				'tried_datetime',
			);
			$this->form_validation->post(array(
				'tried_active' => 'isnull|digit',
				'tried_text' => 'isnull|strip_tags|maxlength:' . $this->settings['tried_maxlength'] . '|minlength:' . $this->settings['tried_minlength'],
				'tried_tags' => 'isnull|jsonencode',
				'tried_image' => '',
			));
			$this->form_validation->_array_data['tried_datetime'] = $this->datetime->_full_date;
			$this->form_validation->fetch();
			$submit = $this->form_validation->submit();
			if ($submit !== true) {
				$this->display_message('danger', \implode('<br>', $this->form_validation->_error), 'admin/trieds/add/', 5);
			}
			$this->trieds_model->addtrieds($this->form_validation->_array_data);
			$this->display_message('success', $this->lang['success_add_data'], 'admin/trieds/view/', 3);
		}

	}
	